﻿Imports DTO
Imports System.Data.SqlClient
Public Class LOAISANHDAO

    Private ketnoi As New DAO.connect()
    Public Function loadloaisanh() As DataTable
        ketnoi.OpenConnection()
        Dim dtnv As New DataTable()
        Dim chuoi As String = "select * from LOAISANH"
        dtnv = ketnoi.getDataTable(chuoi)
        ketnoi.CloseConnection()
        Return dtnv
    End Function

    Public Function loaddssanh() As DataTable
        ketnoi.OpenConnection()
        Dim dtnv As New DataTable
        Dim chuoi As String = "select * from LOAISANH, SANH WHERE LOAISANH.MALOAISANH=SANH.MALOAISANH"
        dtnv = ketnoi.getDataTable(chuoi)
        ketnoi.CloseConnection()
        Return dtnv
    End Function
    Private Function Sqlcommand_THEM(ByVal MALOAISANH As Integer, ByVal TENLOAISANH As String, ByVal DONGIATOITHIEU As Single) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_THEM_LOAISANH", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        'sqlcmd.Parameters.Add("@mode", SqlDbType.Int).Value = mode;
        sqlcmd.Parameters.Add("@MALOAISANH", SqlDbType.Int).Value = MALOAISANH
        sqlcmd.Parameters.Add("@TENLOAISANH", SqlDbType.NVarChar).Value = TENLOAISANH
        sqlcmd.Parameters.Add("@DONGIATOITHIEU", SqlDbType.Float).Value = DONGIATOITHIEU



        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function them_LOAISANH(ByVal dto As LOAISANHDTO)
        ketnoi.OpenConnection()
        'Dim dto As New LOAISANHDTO()
        If Sqlcommand_THEM(dto.MALOAISANH, dto.TENLOAISANH, dto.DONGIATOITHIEU) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False
    End Function

    Private Function Sqlcommand_capnhat(ByVal MALOAISANH As Integer, ByVal TENLOAISANH As String, ByVal DONGIATOITHIEU As Single) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("STOR_CAPNHAT_LOAISANH", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.Add("@MALOAISANH", SqlDbType.Int).Value = MALOAISANH
        sqlcmd.Parameters.Add("@TENLOAISANH", SqlDbType.NVarChar).Value = TENLOAISANH
        sqlcmd.Parameters.Add("@DONGIATOITHIEU", SqlDbType.Float).Value = DONGIATOITHIEU


        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function capnhat_loaisanh(ByVal dto As LOAISANHDTO) As Boolean
        ketnoi.OpenConnection()
        If Sqlcommand_capnhat(dto.MALOAISANH, dto.TENLOAISANH, dto.DONGIATOITHIEU) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False

    End Function

    Private Function Sqlcommand_XOA(ByVal MALOAISANH As Integer) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_XOA_LOAISANH", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.Add("@MALOAISANH", SqlDbType.Int).Value = MALOAISANH

        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function xoa_LOAISANH(ByVal dto As LOAISANHDTO) As Boolean
        ketnoi.OpenConnection()
        If Sqlcommand_XOA(dto.MALOAISANH) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False

    End Function


End Class
